<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">后台管理注册</div>
            <el-form :model="registerForm" :rules="rules" ref="register" label-width="0px" class="ms-content">
                <el-form-item prop="account">
                    <el-input v-model="registerForm.account" placeholder="请输入账号">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input
                        type="password"
                        placeholder="请输入密码"
                        v-model="registerForm.password"
                        @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm">注册</el-button>
                </div>
                <p class="login-tips"><a href="javascript:;" @click="toLogin">返回登录</a></p>
            </el-form>
        </div>
    </div>
</template>

<script>
import axios from 'axios';
import { mapState,mapMutations } from 'vuex';
//import { registerData } from "@/api/index.js";

export default {
    data: function() {
        return {
            registerForm: {
                account: '',
                password: '',
            },
            rules: {
                account: [{ required: true, message: '请输入账号', trigger: 'blur' },
			 { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
			 ],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' },
			 { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
			 ],
            },
        };
    },
    methods: { 
    	...mapMutations(['changeLogin']),//通过子组件定义的方法传递参数，在…mapMutations引用
	toLogin(){
			this.$router.push('/login');
		},

		//账号注册
		  /* async register() {
			const { data: res } = await registerData(this.registerForm)
			if(200==res.code){
				this.$message.success(res);
			}else{
				this.$message.error(res);
			}
		}, */
		  /* register(){
			let that=this;
			axios.post('/login/signin', {
			    data:this.registerForm
			  })
			  .then(function (response) {
				  console.log(response)
				  if(response.code==200){
					  that.$message.success(response.msg);
				  }else{
					  that.$message.error(response.msg);
				  }
				  
			  })
			  .catch(function (error) {
			    that.$message.error(error.msg);
			  });
		}, */
		
		register(){
			let that=this;
			axios({
				method:'post',
				url:'/api/login/signin', //带token验证
				data:that.registerForm  //提交json格式数据
			}).then(res=>{//请求成功时
				//判断json对象为空，Object.keys(res.data.message).length!=0
				console.log(res)
				if (res.data.code==200){
					that.$message.success('注册成功');
					that.$router.push('/login');
				}else{//返回空对象时
					console.log(res)
					that.$message.error('用户名称或密码错误，请重新登陆');		
				}            			
			}).catch(err=>{
				console.log(err)
				that.$message.error('登陆失败！')
			}) 
		},
		
        submitForm() {
		   //this.interceptor()  //拦截请求
		   let that=this; //改变this指向
		   that.$refs.register.validate(valid => {
			   if(valid){
				   that.register();
			   }else{
				   this.$message.error("请输入账号和密码！");
				   return false;
			   }
		   })
		   
        }
    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: black;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>